<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    return view('api');
});

Route::get('/forget-token', function () {

    session()->forget('api-token');
    return redirect('/');

});

Route::get('/redirect', function () {
    //return 1;
    //return config('authApi.API_URL').', '.config('authApi.APP_CLIENT_ID').', '.(config('authApi.API_CLIENT_SECRET'));
    //return config('authApi.API_CLIENT_SECRET');
    //dd(config('authApi.API_URL'), config('authApi.APP_CLIENT_ID'), env('authApi.API_CLIENT_SECRET'));
    if (!config('authApi.API_URL') || !config('authApi.APP_CLIENT_ID') || !config('authApi.API_CLIENT_SECRET')) {
        return config('authApi.API_URL') . ', ' . config('authApi.APP_CLIENT_ID') . ', ' . (config('authApi.API_CLIENT_SECRET'));
        return "Well, in route/web.php, the env param: 'API_CLIENT_ID', 'API_URL', 'API_CLIENT_SECRET' are not all got. Please fill API fields in env file";
        return "Please fill API fields in env file";
    }
//    $query = http_build_query([
//        'client_id' => config('authApi.API_CLIENT_ID'),
//        'response_type' => 'code',
//        'scope' => '',
//    ]);
    $query = http_build_query([
        'client_id' => 12,
        'redirect_uri' => 'http://localhost:8081/callback',
        'response_type' => 'code',
        'scope' => '',
    ]);
    return redirect('http://localhost:8000' . '/oauth/authorize?' . $query);
});

//Route::get('/oauth', redirect('/redirect'));

Route::get('/callback', 'OAuthController@oauth');
//Route::get('/callback', function (Request $request) {
//    $http = new GuzzleHttp\Client;
//    $response = $http->post(config('http://localhost:8000') . '/oauth/token', [
//        'form_params' => [
//            'grant_type' => 'authorization_code',
//            'client_id' => 12,
//            'client_secret' => 'dqNg2Y3X9veOAttCZa7JGGWWIqupFzIBBzjNKf7o',
//
//            'code' => $request->code,
//        ],
//    ]);
//
//    $apiResponse = json_decode((string)$response->getBody(), true);
//    session(['api' => $apiResponse]);
//    session(['api-token' => $apiResponse['access_token']]);
//    return redirect('/');
//});
